#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include<bits/stdc++.h>
//#pragma GCC target("popcnt")
using namespace std;
#define int long long
int ttt;
int n;int K;
int ar[500005];
void solve(){
cin>>n>>K;
for(int i=1;i<=n;i++){
cin>>ar[i];
ar[i]%=K;
}
priority_queue<int,vector<int>,greater<int>>pq;
int ans=0;
int nw=0;
for(int i=1;i<=n;i++){
int dv=nw/K;
//if(ar[i]!=0){
//if(ar[i]==ar[i-1]&&i>=2){continue;}
if(ar[i]<=nw){
int v=dv*K+ar[i];
if(v<=nw){
pq.push((dv+1)*K+ar[i]-nw);
// cout<<(dv+1)*K+ar[i]-nw<<" ";
nw=v;
}else{
pq.push(dv*K+ar[i]-nw);
// cout<<dv*K+ar[i]-nw<<" ";
v=(dv-1)*K+ar[i];
nw=v;
}
}else{
int v=ar[i];
int c=v-nw;
if(pq.size()&&pq.top()<c){
c=pq.top();
pq.pop();
pq.push(v-nw);
}
nw=ar[i];
ans+=c;
}
//}
//cout<<nw<<" "<<ans<<endl;
}
cout<<ans<<endl;
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>ttt;
while(ttt--)solve();
}
1176A - Divide it | 1527A - And Then There Were K |
1618E - Singers' Tour | 1560B - Who's Opposite |
182B - Vasya's Calendar | 934A - A Compatible Pair |
1618F - Reverse | 1684C - Column Swapping |
57C - Array | 1713D - Tournament Countdown |
33A - What is for dinner | 810A - Straight A |
1433C - Dominant Piranha | 633A - Ebony and Ivory |
1196A - Three Piles of Candies | 299A - Ksusha and Array |
448B - Suffix Structures | 1092B - Teams Forming |
1166C - A Tale of Two Lands | 544B - Sea and Islands |
152B - Steps | 1174D - Ehab and the Expected XOR Problem |
1511A - Review Site | 1316A - Grade Allocation |
838A - Binary Blocks | 1515D - Phoenix and Socks |
1624D - Palindromes Coloring | 1552F - Telepanting |
1692G - 2Sort | 1191A - Tokitsukaze and Enhancement |